<?php

	class TypeAction extends CommonAction {
			/**
			 * 显示商品类型
			*/
			public function index (){
				$db = M('type');
				import("ORG.Util.Page");
				$count = $db->count();
				$Page = new Page($count,10);
				$show = $Page->show();
				$arr = $db->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
				$this->assign('typeData',$arr);
				$this->assign('page',$show);
				$this->display();
			}
			/**
			 *添加商品类型
			 */
			public function addType(){
				$db = M('type');
				if($db->where(array('name'=>$_POST['name']))->find()){
					echo json_encode(array('num'=>0,'mesa'=>'类型已存在'));
					die;
				}else{
					$id = $db->add(array('name'=>$_POST['name']));
					if($id){
						echo json_encode(array('num'=>1,'mesa'=>'插入成功'));
					}else{
						 echo json_encode(array('num'=>0,'mesa'=>'插入成功'));
					}
				}
			}

			/**
			 * 显示商品类型属性表
			 */
			public function typeArticle(){
				$db = D('TypeatributeView');
				import("ORG.Util.Page");
				$count = $db->count();
				$Page = new Page($count,10);
				$show = $Page->show();
				$arr = $db->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
				$this->assign('typeData',$arr);
				$this->assign('page',$show);
				$this->display();
			}
			/**
			 * 添加类型属性
			 */
			public function article(){
				$db = M('type');
				$typeData = $db->select();
				if(isset($_GET['tid'])){
					$type = $db->where(array('id'=>(int)$_GET['tid']))->find();
				}else{
					$type = array(
							'id'=>0,
							'name'=>'选择类型'
						);
				}
				$this->assign('typeData',$typeData);
				$this->assign('type',$type);
				$this->display();
			}
			/**
			 * 添加类型熟悉
			 */
			public function addArticle(){
				$db = M('typeatribute');
				$where = array(
					'tid' => (int) $_POST['tid'],
					'name' => $_POST['name']
				);	
				if($db->where($where)->find()){
					echo json_encode(array('num'=>0,'mesa'=>'同类型下的属性已存在'));
					die;
				}else{
					$_POST['type'] = $_POST['style'];
					$_POST['cont'] = str_replace(PHP_EOL,',',$_POST['check']);
					if($db->add($_POST)){
						echo json_encode(array('num'=>1,'mesa'=>'插入成功'));
					}else{
						echo json_encode(array('num'=>0,'mesa'=>'插入失败')); 
					}
				}
			}
	}
?>